package com.lgx.mobile.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.mobile.dao.ITopicDao;
import com.lgx.mobile.entity.Topic;
import com.lgx.mobile.service.ITopicService;
import com.lgx.mobile.vo.TopicVO;
import com.lgx.right.dao.IUserDao;
import com.lgx.right.entity.User;

/**
 * 话题Service
 * 
 * @author xuyichen
 *
 */
@Service("topicService")
public class TopicService implements ITopicService {

	@Autowired
	private ITopicDao topicDao;

	public ITopicDao getTopicDao() {
		return topicDao;
	}

	public void setTopicDao(ITopicDao topicDao) {
		this.topicDao = topicDao;
	}
	
	@Autowired
	private IUserDao userDao;

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	@Transactional
	public Topic createTopic(Topic topic) throws DataException {
		return this.topicDao.createTopic(topic);
	}

	@Transactional
	public Topic updateTopic(Topic topic) throws DataException {
		return this.topicDao.updateTopic(topic);
	}

	@Transactional
	public void deleteTopic(Topic topic) throws DataException {
		this.topicDao.deleteTopic(topic);
	}

	@Transactional
	public void deleteTopicById(long id) throws DataException {
		this.topicDao.deleteTopicById(id);
	}

	public Topic findTopicById(long id) {
		return this.topicDao.findTopicById(id);
	}
	
	public Topic checkTopicExists(long createUserId,Date createDate,long categoryId){
		return this.topicDao.checkTopicExists(createUserId,createDate,categoryId);
	}

	public List<Topic> findTopicList() {
		return this.topicDao.findTopicList();
	}
	
	public List<Topic> findTopicListByCategoryId(long categoryId){
		return this.topicDao.findTopicListByCategoryId(categoryId);
	}
	
	public List<TopicVO> findHotTopicList(int num){
		return this.topicDao.findHotTopicList(num);
	}

	@Override
	public Pagination findTopicPaginationByCategoryId(long categoryId, Object[] params, Class<?> clazz, int pageSize, int pageNum) {
		return this.topicDao.findTopicPaginationByCategoryId(categoryId,params,clazz,pageSize,pageNum);
	}

	@Override
	public TopicVO findTopicVOByTopicId(long topicId) {
		TopicVO topicVO = new TopicVO();
		Topic topic = this.findTopicById(topicId);
		if(topic != null){
			topicVO.setId(topic.getId());
			topicVO.setName(topic.getName());
			topicVO.setTopic(topic.getTopic());
			topicVO.setCreateDate(topic.getCreateDate());
			topicVO.setCategoryId(topic.getCategoryId());
			topicVO.setTopicImage(topic.getTopicImage());
			topicVO.setCreateUserId(topic.getCreateUserId());
			topicVO.setUserNum(topic.getUsers().size());
			User createUser = this.userDao.findUserById(topic.getCreateUserId());
			if(createUser != null){
				topicVO.setCreateUserNickName(createUser.getNickname());
				topicVO.setCreateUserName(createUser.getUsername());
				topicVO.setCreateUserTruename(createUser.getTruename());
				topicVO.setCreateUserEmail(createUser.getEmail());
				topicVO.setCreateUserSex(createUser.getSex());
				topicVO.setCreateUserMobile(createUser.getMobile());
				topicVO.setCreateUserCardId(createUser.getCard().getId());
			}else{
				topicVO.setCreateUserNickName("");
				topicVO.setCreateUserName("");
				topicVO.setCreateUserTruename("");
				topicVO.setCreateUserEmail("");
				topicVO.setCreateUserSex(0);
				topicVO.setCreateUserMobile("");
				topicVO.setCreateUserCardId("");
			}
			return topicVO;
		}
		return null;
	}
}
